import 'package:number_spark_flutter/config/ns_base_controller.dart';
import 'package:number_spark_flutter/model/ns_user_info_model.dart';
import 'package:number_spark_flutter/network/ns_api.dart';
import 'package:number_spark_flutter/network/ns_network_utils.dart';
import 'package:number_spark_flutter/utils/ns_dialog_util.dart';
import 'package:number_spark_flutter/utils/ns_log_report_utils.dart';
import 'package:number_spark_flutter/utils/ns_permission_util.dart';
import 'package:number_spark_flutter/utils/ns_toast_utils.dart';
import 'package:number_spark_flutter/utils/ns_user_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

import '../../network/dio_log/dio_log.dart';
import '../../routes/ns_app_pages.dart';
import '../../utils/ns_current_limiting_util.dart';
import 'view/record_bottom_sheet/record_bottom_sheet_view.dart';

const String idRecordModel = 'recordModel';

class NSMineLogic extends NSBaseController {
  NSUserInfoModel userInfoModel = NSUserInfoModel();
  int selectedIndex = 0;
  PageController pageController = PageController();
  List<NSUserInfoModel> blockUserList = [];

  @override
  void onInit() {
    super.onInit();
    onGetUserInfo();
  }

  onGetUserInfo() async {
    Map<String, dynamic> params = {
      'userId': NSUserUtils.getId(),
    };
    NSResponseData responseData = await NSApi.getUserInfo.get(params);
    if (responseData.isSuccess()) {
      userInfoModel = NSUserInfoModel.fromJson(responseData.data);
      update();
      // NSUserUtils.saveUserInfo(responseData.data);
    }
  }

  doClickCopy() {
    Clipboard.setData(ClipboardData(text: userInfoModel.userId));
    NSToast.show("Copy Success");
  }

  onPageChanged(index, {bool isClick = false}) {
    selectedIndex = index;
    if (isClick) {
      pageController.jumpToPage(selectedIndex);
    }
    update();
    if (selectedIndex == 0) {
    } else if (selectedIndex == 1) {}
  }

  doClickUserInfo() {
    Get.toNamed(NSRoutes.settingUserInfo)?.then((value) {
      // if (value != null && value) {
      onGetUserInfo();
      // }
    });
  }

  doClickRecord() {
    NSPermissionUtil.reqMicrophonePermission(onSuccessAction: () {
      Get.bottomSheet(NSRecordBottomSheetPage(), isScrollControlled: false).then((value) async {
        if (value != null) {
          await NSUserUtils.setUserRecordModel(value);
          update([idRecordModel]);
        }
      });
    });
  }
}
